package com.apestech.formwork.condition;

import com.apestech.framework.db.SqlSession;
import com.apestech.framework.json.JSONArray;
import com.apestech.framework.json.JSONObject;
import com.apestech.framework.sql.callback.SaveTableCallback;
import com.apestech.framework.util.DateUtil;
import com.apestech.framework.xml.plugin.modul.SpecialField;
import com.apestech.framework.xml.table.modul.Tables;

import java.util.Date;

/**
 * @author hup
 * @create 2019-01-30-19:13
 */
public class ExecDimension implements SaveTableCallback {

    private Tables tables;
    private String state;
    private String personId;

    public ExecDimension(Tables tables, String state, String personId){
        this.tables=tables;
        this.state=state;
        this.personId=personId;
    }

    @Override
    public boolean before(String tabName) throws Exception {
        return true;
    }

    @Override
    public void write(String tabName, JSONObject input, int index, SqlSession session) throws Exception {
        String timefield=tables.getTimeField();
        SpecialField.Field sysField= session.getSpecialField();
        if(tabName.equals(tables.getMasterTable())) {
            String sql=null;
            JSONObject m=new JSONObject();
            if("exec".equals(state)){
                sql="UPDATE "+tabName+" SET "+sysField.getApproveId()+"=#{"+sysField.getApproveId()+"},"+
                        sysField.getApproveDate()+"=#{"+sysField.getApproveDate()+"},"+
                        sysField.getState()+"=#{"+sysField.getState()+"},"+
                        timefield+"=#{"+timefield+"_NEW} WHERE ID=#{ID} AND GET_TIMESTAMP("+timefield+")=#{"+timefield+"_OLD} ";
                m.put(sysField.getApproveId(), personId);
                m.put(sysField.getApproveDate(),"SYSDATE");
            } else if("stop".equals(state)){
                sql="UPDATE "+tabName+" SET "+sysField.getStopId()+"=#{"+sysField.getStopId()+"},"+
                        sysField.getStopDate()+"=#{"+sysField.getStopDate()+"},"+
                        sysField.getState()+"=#{"+sysField.getState()+"},"+
                        timefield+"=#{"+timefield+"_NEW} WHERE ID=#{ID} AND GET_TIMESTAMP("+timefield+")=#{"+timefield+"_OLD} ";
                m.put(sysField.getStopId(), personId);
                m.put(sysField.getStopDate(),"SYSDATE");
            }
            m.put(sysField.getState(),state);
            m.put("ID",input.getString("ID"));
            m.put(timefield+"_NEW","SYSDATE");
            m.put(timefield+"_OLD", input.get(timefield));
            m.put("autoCompletion","0");
            if(session.getForUpdate().update(sql, m)==0){
                throw new Exception("该数据已被修改或者删除，不允许继续操作！");
            }
            m=new JSONObject();
            m.put("ID",input.getString("ID"));
            m.put("autoCompletion","0");
            session.getForUpdate().update("DELETE "+tables.getExecTable()+" WHERE 1=1 AND ID=#{ID} ", m);
        }else{
            if("exec".equals(state)){
                JSONObject map=new JSONObject(input);
                map.putAll(tables.getSelectMap());
                String tab=tables.getExecTable();
                JSONArray list=session.getForJSON().table(tab, map);
                if(list.size()==0){
                    map.put(timefield, "SYSDATE");
                    map.put(sysField.getState(),state);
                    session.getForUpdate().insertTable(tab, map);
                }
                else {
                    map=new JSONObject(input);
                    map.putAll(tables.getTimeMap());
                    map.put(timefield+"_NEW", "SYSDATE");
                    Object st=list.get(0).get(timefield);
                    if(st instanceof String) map.put(timefield+"_OLD", st);
                    else map.put(timefield+"_OLD", (DateUtil.getTimeStampStr((Date) st)));
                    map.put(sysField.getState(),state);
                    session.getForUpdate().saveTable(tab, map);
                }
            }
        }
    }

    @Override
    public String after(String tabName, JSONObject input) throws Exception {
        return null;
    }
}
